import { ref, onMounted, watch } from 'vue';
import { page, search } from '@/chaos/functions/common/Data';

export default function useTableDatas(searchMethod, data = {}, currentPage = 1, limit = 20) {
    const domain = 'iya_user';

    const tableDatas = ref([]);
    const total = ref(0);
    const loading = ref(false);

    const getTableDatas = async () => {
        loading.value = true;
        let res;
        if (searchMethod) {
            res = await search(domain + '/' + searchMethod, currentPage, limit, data);
        } else {
            res = await page(domain, currentPage, limit, data);
        }
        const { list, total: _total } = res;
        tableDatas.value = list;
        total.value = _total;
        loading.value = false;
    };

    onMounted(getTableDatas);

    watch(data, getTableDatas);
    watch(currentPage, getTableDatas);
    watch(limit, getTableDatas);

    return { tableDatas, total, loading };
}
